package oc_redis

import (
	"fmt"
	"log"
	"oc_saas/internal/glo"
	"os"
	"time"

	"github.com/go-redis/redis"
)

type ocRedis struct{}

// 搭建redis集群
func (*ocRedis) InitRedisCluster() {
	glo.OC_REDIS = redis.NewClusterClient(&redis.ClusterOptions{
		Addrs: []string{
			// 这里是redis集群的映射ip 有多个集群
			"172.25.176.101:7001",
			"172.25.176.102:7002",
			"172.25.176.103:7003",
			"172.25.176.104:7004",
			"172.25.176.105:7005",
			"172.25.176.106:7006",
		},
		Password:           "0OeEvyEFRMRP04xmk8QSxI2a1ZeMn9Od",
		PoolSize:           10,
		MinIdleConns:       20,
		DialTimeout:        5 * time.Second,  //建立连接超时时间
		ReadTimeout:        3 * time.Second,  //读超时
		WriteTimeout:       3 * time.Second,  //写超时
		PoolTimeout:        4 * time.Second,  //当所有链接处于繁忙，客户端等待可用连接最大等待时间，默认读超时+1秒
		IdleTimeout:        5 * time.Minute,  //闲置超时
		IdleCheckFrequency: 60 * time.Second, //闲置连接检查的周期，默认一分钟，-1表示不检查
		MaxConnAge:         0 * time.Second,  //连接存活时长，从创建开始计时，超过指定时长则关闭，默认为0，不关闭存活时长较长的连接

		// 命令执行失败重试策略
		MaxRedirects:    10,                   //最多重试多少次
		MinRetryBackoff: 8 * time.Microsecond, //每次计算重试间隔时间的下限
		MaxRetryBackoff: 512 * time.Microsecond,

		// 支持TLS接入 //开启后只能通过TLS连接
		// TLSConfig: &tls.Config{
		// 	InsecureSkipVerify: false,
		// },

		ReadOnly:       false,
		RouteRandomly:  true,
		RouteByLatency: false,
	})
	str, err := glo.OC_REDIS.Ping().Result()
	if err != nil {
		log.Fatal(err)
		os.Exit(1)
	}
	fmt.Println("redis集群:ping通 ", str)
}

var OcRedisApp = new(ocRedis)
